package com.yoomiito.app.ui.songduoduo

import android.content.Context
import android.graphics.Typeface
import androidx.recyclerview.widget.RecyclerView
import android.text.Spannable
import android.text.SpannableString
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.model.ViewType
import com.yoomiito.app.model.sdd.SddVipGoodsInfo
import com.yoomiito.app.utils.*
import com.yoomiito.app.widget.CouponView

class SendMoreGoodsAdapter(val mContext: Context,var data:MutableList<ViewType<SddVipGoodsInfo>>?) : androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>(){
    companion object{
        const val BigImageType = 0
        const val SmallImageType = 1
    }
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return if (BigImageType == p1) {
            val view: View = LayoutInflater.from(mContext).inflate(R.layout.item_goods_11, p0, false)
            GoodsViewHolder1(view)
        } else {
            val view: View = LayoutInflater.from(mContext).inflate(R.layout.item_goods_12, p0, false)
            GoodsViewHolder2(view)
        }
    }

    override fun getItemCount(): Int = data?.size?:0

    override fun onBindViewHolder(p0: androidx.recyclerview.widget.RecyclerView.ViewHolder, p1: Int) {
        val item = data?.get(p1)?.t
        when(data?.get(p1)?.viewType){
            BigImageType->{
                val holder = p0 as GoodsViewHolder1
                ImageLoader.getInstance().into(mContext,item?.banner,holder.goodsPic)
                holder.goodsTitle.text = item?.productName
                holder.goodsFlag.text = item?.productPoint
                holder.goodsFlag.visibility = if (SpannableUtils.isEmptyOr0(item?.productPoint))View.GONE else View.VISIBLE
                holder.awardView.setText("¥${SpannableUtils.handlePrice(item?.commission)}")
                holder.awardView.visibility =  if (SpannableUtils.isEmptyOr0(item?.commission)) View.GONE else View.VISIBLE
                holder.couponView.visibility = View.GONE
                val price = "¥${SpannableUtils.handlePrice(item?.salePrice)}"
                val spanText = SpannableString(price)
                spanText.setSpan(RelativeSizeSpan(0.65f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                spanText.setSpan(StyleSpan(Typeface.BOLD), 1, price.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                holder.goodsPrice.text = spanText
                holder.goodsSaleSize.text = "已售${item?.volume}"
                holder.buy.background = ColorUtils.gradientAndRadius("#FFFF035B","#FFFF246C",15.5f)
                holder.view.setOnClickListener { RouterUtils.toGoodsDetail(mContext,item?.id,Type.GoodsType.GiveMore) }
            }
            SmallImageType->{
                val holder = p0 as GoodsViewHolder2
                ImageLoader.getInstance().into(mContext,item?.productImg,holder.goodsPic)
                holder.goodsTitle.text = item?.productName
                holder.awardView.setText("¥${SpannableUtils.handlePrice(item?.commission)}")
                holder.awardView.visibility =  if (SpannableUtils.isEmptyOr0(item?.commission)) View.GONE else View.VISIBLE
                holder.couponView.visibility = View.GONE
                val price = "¥${SpannableUtils.handlePrice(item?.salePrice)}"
                val spanText = SpannableString(price)
                spanText.setSpan(RelativeSizeSpan(0.65f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                spanText.setSpan(StyleSpan(Typeface.BOLD), 1, price.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                holder.goodsPrice.text = spanText
                holder.goodsSaleSize.text = "已售${item?.volume}"
                holder.view.setOnClickListener { RouterUtils.toGoodsDetail(mContext,item?.id,Type.GoodsType.GiveMore) }

            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return data?.get(position)?.viewType!!
    }

    fun newData(data:MutableList<ViewType<SddVipGoodsInfo>>?){
        this.data = data
        notifyDataSetChanged()
    }

    fun addData(data:MutableList<ViewType<SddVipGoodsInfo>>?){
        if (data != null) {
            this.data?.addAll(data)
            notifyItemRangeInserted(this.data?.size?.minus(data.size)!!,data.size)
        }
    }

    inner class GoodsViewHolder1(view:View): androidx.recyclerview.widget.RecyclerView.ViewHolder(view){
        val goodsPic = view.findViewById<ImageView>(R.id.goodsPic)
        val goodsTitle = view.findViewById<TextView>(R.id.goodsTitle)
        val awardView = view.findViewById<CouponView>(R.id.awardView)
        val couponView = view.findViewById<CouponView>(R.id.couponView)
        val goodsPrice = view.findViewById<TextView>(R.id.goodsPrice)
        val goodsSaleSize = view.findViewById<TextView>(R.id.goodsSaleSize)
        val buy = view.findViewById<TextView>(R.id.buy)
        val goodsFlag = view.findViewById<TextView>(R.id.goodsFlag)
        val view = view.findViewById<View>(R.id.goodsView)
    }

    inner class GoodsViewHolder2(view:View): androidx.recyclerview.widget.RecyclerView.ViewHolder(view){
        val goodsPic = view.findViewById<ImageView>(R.id.goodsPic)
        val goodsTitle = view.findViewById<TextView>(R.id.goodsTitle)
        val awardView = view.findViewById<CouponView>(R.id.awardView)
        val couponView = view.findViewById<CouponView>(R.id.couponView)
        val goodsPrice = view.findViewById<TextView>(R.id.goodsPrice)
        val goodsSaleSize = view.findViewById<TextView>(R.id.goodsSaleSize)
        val view = view.findViewById<View>(R.id.goodsView)
    }

}